package com.tpddns.flannery.cirecleoffriends;

import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.tpddns.flannery.cirecleoffriends.service.AccessService;
import com.tpddns.flannery.cirecleoffriends.service.FloatWindowService;
import com.yalantis.ucrop.UCrop;


public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_MEDIA_PROJECTION = 18;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.checkPermission).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPermission();
                requestCapturePermission();
            }
        });

        findViewById(R.id.startService).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPermission();
            }
        });

        findViewById(R.id.uCrop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //截图功能
//                UCrop.of(sourceUri, destinationUri)
//                        .withAspectRatio(16, 9)
//                        .withMaxResultSize(maxWidth, maxHeight)
//                        .start(context);
            }
        });

        findViewById(R.id.jump2WeiChat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppUtils.getWechatApi(MainActivity.this);
                AppUtils.IS_JUMP_FROM_ME = true;
            }
        });

        findViewById(R.id.checkAccessibility).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean accessibilitySettingsOn = AppUtils.isAccessibilitySettingsOn(MainActivity.this);
                Toast.makeText(MainActivity.this, accessibilitySettingsOn ? "有无障碍权限" : "没有无障碍权限", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.checkServiceIsRunning).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean b = AppUtils.serviceIsRunning(MainActivity.this);
                Toast.makeText(MainActivity.this, b ? "有无障碍服务" : "没有无障碍服务", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 截屏权限
    public void requestCapturePermission() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //5.0 之后才允许使用屏幕截图

            return;
        }

        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(
                mediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }

    private void initPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (Settings.canDrawOverlays(MainActivity.this)) {
                Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
                Toast.makeText(MainActivity.this, "已开启Toucher", Toast.LENGTH_SHORT).show();
                startService(intent);
                //finish();
            } else {
                //若没有权限，提示获取.
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                Toast.makeText(MainActivity.this, "需要取得权限以使用悬浮窗", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        } else { //SDK在23以下，不用管
            Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
            //startService(intent);
            //finish();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
        } else if (resultCode == UCrop.RESULT_ERROR) {
            final Throwable cropError = UCrop.getError(data);
        }

        switch (requestCode) { //截屏权限
            case REQUEST_MEDIA_PROJECTION:

                if (resultCode == RESULT_OK && data != null) {
                    AccessService.setResultData(data);
                    startService(new Intent(getApplicationContext(), AccessService.class));
                }
                break;
        }
    }
}
